package com.mmlc.meimei.lichengkongjian.customview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.BannerResponseBean;

import java.util.List;


/**
 * Created by Administrator on 2017/1/18.
 */

public class AdDialog extends Dialog{
    //
    private Context context;
    List<BannerResponseBean.AdListBean> adList;
    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {

        public void doAd();

        public void doCancel();
    }
    public AdDialog(Context context,int theme,List<BannerResponseBean.AdListBean> adList) {
        super(context,theme);
        this.context = context;
        this.adList = adList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.ad_dialog, null);
        setContentView(view);
        ImageView iv_ad = (ImageView) view.findViewById(R.id.iv_ad);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        iv_ad.setOnClickListener(new clickListener());
        iv_cancel.setOnClickListener(new clickListener());
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 1); // 高度设置为屏幕的0.6
        lp.height = (int)(d.heightPixels*1);
        dialogWindow.setAttributes(lp);
        if(adList.size()>0){
            if(!TextUtils.isEmpty(adList.get(0).getImg())){
//                XUtils.image(iv_ad,adList.get(0).getImg());
                Glide.with(context).load(adList.get(0).getImg()).error(R.drawable.placeholder).into(iv_ad);
            }
        }
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {
                case R.id.iv_ad:
                    clickListenerInterface.doAd();
                    break;
                case R.id.iv_cancel:
                    clickListenerInterface.doCancel();
                    break;
            }
        }

    };
}
